<?php
/*
 * This file is part of SoftSite.
 *
 * (c) 2012 Steve Tredinnick
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 *
 * @package softsite
 * @author  Steve Tredinnick <steve.tredinnick@gmail.com>
 */
namespace Classes;

use Classes\Http;
use Classes\BaseView;

/**
 * BaseController class provides all base functionality to the controller classes
 * and extends into BaseView to allow all templating functionality.
 */
abstract class BaseController extends BaseView
{
    /**
     * @var Http Variable to hold the HTTP Helper class instance.
     */
    protected $http;

    /**
     * Constructor for the BaseController class.
     * @param $action String The specified action that the controller is being requested for.
     * @param $urlValues Array The url values that are being passed to the controller
     */
    public function __construct($action, $urlValues)
    {

        //invoke the constructor for BaseView.
        parent::__construct($action, $urlValues);

        //Set the default values for the Controller.
        $this->urlValues = $urlValues;
        $this->action = $action;
        $this->id = $this->GetId();
        $this->controller = $this->GetController();

        //Create an instance of the URL Helper.
        $this->http = new Http();
    }


    /**
     * @Desc Executes the requested action within the controller.
     * @return string Requested Action
     */
    public function ExecuteAction()
    {
        return $this->{$this->action}();
    }

    /**
     * Gets the requested controller name from the URL array.
     * @return string Controller from the URL array
     */
    protected function GetController()
    {
        //return a default home controller if the controller specified in the url does not exist
        if($this->urlValues['controller']=="")
        {
            return "home";
        }
        else
        {
            return $this->urlValues['controller'];
        }

    }

    /**
     * Gets the requested action from the URL array.
     * @return string Action from the URL array
     */
    protected function GetAction()
    {
        return $this->urlValues['action'];
    }

    /**
     * Gets the requested id from the URL array.
     * @return string ID from the URL array
     */
    protected function GetId()
    {
        return $this->urlValues['id'];
    }

}